package com.example.moneygrosService.handlers;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;

import com.example.moneygrosService.MainActivityService.DB_TOKEN;
import com.example.moneygrosService.User;
import com.example.moneygrosService.database.MoneyContract;
import com.example.moneygrosService.database.MoneyContract.Users;
import com.example.moneygrosService.handlers.ResultListener.ResultType;

public class DeleteContactHandler extends AsyncQueryHandler {
	private static final String TAG = "DeleteContactHandler";
	
	private Bundle mBundle;
	private User mUser;
	private ContentResolver mCr;
	
	private boolean mUserDeleted=false;
	private boolean mResult;
	
	private final Lock mLock = new ReentrantLock();
	private Condition mResultOKCond = mLock.newCondition();
	private boolean mResultOK;
	
	private ResultListener mListener;
	
	private Uri mUserUri;
	
	public DeleteContactHandler(ContentResolver cr, Uri uri) {
		super(cr);
		// TODO Auto-generated constructor stub
		mCr= cr;
		mUserUri = uri;
	}

	public DeleteContactHandler(ContentResolver cr, User user) {
		super(cr);
		// TODO Auto-generated constructor stub
		mCr= cr;
		mUser = user;
	}
	
	public void delUser(){
		mResultOK=false;
		
		CheckAndDeleteUserHandler checkAndDeleteUserHandler = new CheckAndDeleteUserHandler(mCr);
        checkAndDeleteUserHandler.checkAndDelete();
        
	}


	protected void userDeleted(boolean delete){
		mResultOK = true;
		mResult = delete;
		mUserDeleted = delete;
		mListener.onResult(delete, ResultType.USER_DELETED, null);
	}
	
	public class CheckAndDeleteUserHandler extends AsyncQueryHandler {
		private static final String TAG = "DeleteContactHandler";
		
		private void deleteUser() {			
			if(mUser != null){		
				String[] selectionArgs= {mUser.getUser_id()};
//				String selection = MoneyContract.UsersColumns.USER_ID+"=?";
				startDelete(DB_TOKEN.DELETE_USER, (Object) mUser, Users.CONTENT_URI, null, selectionArgs);
				
			} else {
				startDelete(DB_TOKEN.DELETE_USER, null, mUserUri, null, null);
				
			}
		}

		public CheckAndDeleteUserHandler(ContentResolver cr) {
			super(cr);
		}
		
		public void checkAndDelete(){
			String[] projection = {BaseColumns._ID, MoneyContract.UsersColumns.USER_ID};
			
			if(mUser != null){
				String[] selectionArgs= {mUser.getUser_id()};
				String selection = MoneyContract.UsersColumns.USER_ID+"=?";
				startQuery(DB_TOKEN.CHECK_USER_FOR_DELETE, (Object) mUser, Users.CONTENT_URI, projection, selection, selectionArgs, null);
				
			} else {
				startQuery(DB_TOKEN.CHECK_USER_FOR_DELETE, null, mUserUri, projection, null, null, null);
			}
		}

		@Override
		protected void onDeleteComplete(int token, Object cookie, int result) {
			// TODO Auto-generated method stub
			Log.v(TAG, "listener int");
//			updateList();
		}

		/* (non-Javadoc)
		 * @see android.content.AsyncQueryHandler#onQueryComplete(int, java.lang.Object, android.database.Cursor)
		 */
		@Override
		protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
			// TODO Auto-generated method stub
    		switch(token){

    		case DB_TOKEN.CHECK_USER_FOR_DELETE:
    			Log.v("CheckAndDeleteUserHandler", "CHECK_USER_FOR_DELETE");
    			if(cursor != null){
    				if(cursor.getCount() != 0){
    					cursor.close();
    	    			Log.v("CheckAndDeleteUserHandler", "deleting user");
    					deleteUser();
//    					insertUser((User)cookie);
    				} else {
    	    			Log.v("CheckAndDeleteUserHandler", "no user");
//    	    			Toast.makeText(context, "existing user", Toast.LENGTH_SHORT).show();
    	    			if(mListener!=null)
    	    				mListener.onResult(false, ResultType.EXISTING_USER, null);
    				}
    			}    				
        		break;
    		}
		}

		/* (non-Javadoc)
		 * @see android.content.AsyncQueryHandler#startQuery(int, java.lang.Object, android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)
		 */
		@Override
		public void startQuery(int token, Object cookie, Uri uri,
				String[] projection, String selection, String[] selectionArgs,
				String orderBy) {
			// TODO Auto-generated method stub
			super.startQuery(token, cookie, uri, projection, selection, selectionArgs,
					orderBy);
		}
		
	}

	public void addResultListener(ResultListener obj) {
		// TODO Auto-generated method stub
		mListener = obj;		
	}

}
